/*
 * Copyright 2009 Eat Local Food, LLC
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.common.client.components.CommonSimpleComponent;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;


/**
 * Simple Display Component
 */
public abstract class StoreSimpleComponent extends CommonSimpleComponent
{
	protected StoreComponentController storeController;
	
	protected Image rightTitleImage;
	protected InlineLabel leftTitleLabel;
	
	protected Grid panel;

	/**
	 * Constructor
	 */

	public StoreSimpleComponent(StoreComponentController controller, String stylePrefix, String strRightImage, boolean linkRightImage)
	{
		super(controller, stylePrefix);
		if (stylePrefix == null) stylePrefix = "";
		storeController = controller;
		
		String title = controller.getComponentTitle(handle);
		if (title == null) title = handle;
		
		panel = new Grid(2, 1);
		panel.setStyleName(stylePrefix + "ComponentPanel");
		panel.addStyleName(handle + "Panel");
		CellFormatter cf = panel.getCellFormatter();
		cf.setStyleName(0, 0, stylePrefix + "ComponentTitle");
		cf.addStyleName(0, 0, handle + "Title");
		cf.setStyleName(1, 0, stylePrefix + "ComponentBody");
		cf.addStyleName(1, 0, handle + "Body");
		
		add(panel);
		
		thisComponent = this;

		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setWidth("100%");
		leftTitleLabel = new InlineLabel(title);
		leftTitleLabel.setStyleName(stylePrefix + "ComponentTitle");
		leftTitleLabel.addStyleName(handle + "Title");
		hpanel.add(leftTitleLabel);
		
		if (strRightImage != null && strRightImage.length() > 0)
		{
			rightTitleImage = new Image();
			rightTitleImage.setUrl(getImageCommonDirectory() + strRightImage);
			if (linkRightImage) rightTitleImage.setStyleName("ComponentLink");
			hpanel.add(rightTitleImage);
			hpanel.setCellHorizontalAlignment(rightTitleImage, HasHorizontalAlignment.ALIGN_RIGHT);
		}
		else
		{	
			rightTitleImage = null;
		}
		
		panel.setWidget(0, 0, hpanel);
	}
	
	public String getTitle()
	{
		return leftTitleLabel.getText();
	}

	public InlineLabel getLeftTitleLabel() {
		return leftTitleLabel;
	}

	public Image getRightTitleImage() {
		return rightTitleImage;
	}
	
	public boolean updateFromHistoryParameters(String[] params)
	{
		storeController.getMultipleResultsComponent().addComponent(this);
		return super.updateFromHistoryParameters(params);
	}
	
	protected void buildFromException(Throwable exception)
	{
		panel.setText(1, 0, exception.toString());
	}

	protected String getLanguageCode()
	{
		return StoreComponentController.languagesComponent.getLanguageModel().getCode();
	}

}
